home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18035 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  97 lines

  1. Path: news.halcyon.com!usenet
  2. From: normanb@halcyon.com (Norm Bryar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Pure virtual destructors?
  5. Date: Thu, 18 Apr 1996 16:31:08 GMT
  6. Organization: Northwest Nexus Inc.
  7. Message-ID: <4l5qmv$d0t@news.halcyon.com>
  8. References: <4kuq0i$p6t@ftp.ee.vill.edu> <3175BDF5.5ADC81B5@eiffel.com>
  9. NNTP-Posting-Host: blv-pm3-ip12.halcyon.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. "Guus Leeuw jr." <guusl@eiffel.com> wrote:
  13.  
  14. >sheridan@monet.vill.edu wrote:
  15. >> 
  16. >> The rule with destructors is that they are always overridden, right?
  17. >> That's a quote from VC4 docs, BTW.  When I do this:
  18. >> 
  19. >> class CBase {
  20. >> public:
  21. >>     virtual ~CBase() = 0;
  22. >> ...};
  23. >> 
  24. >> class CChild : public CBase {
  25. >> public:
  26. >>     ~CChild();
  27. >> ...};
  28. >> 
  29. >> and declare a body for ~CChild, I always get unresolved external on
  30. >> CBase::~CBase.  I have seen this in several cases.  My guess is it's not
  31. >> a compiler bug, so what am I missing?
  32.  
  33. >Here is what the working paper of the ANSI comittee on destructors says (sec.
  34. >12.4.6):
  35. >"Destructors are not inherited. A destructor can be declared virtual or pure
  36. >virtual; if any objects of that class or any derived class are created in the
  37. >program, the destructor shall be defined. If a class has a base class with a
  38. >virtual destructor, its destructor (whether user- or implicitly-declared) is
  39. >virtual."
  40.  
  41. >Which means: You cannot have a pure virual destructor in CBase. It can be
  42. >virtual, though, for a virtual destructor still comes with a definition.
  43.  
  44. >-- 
  45. >'l8r,
  46. >    Guus
  47.  
  48. You *can* have a pure virtual destructor in CBase.  How did you
  49. possibly read that you couldn't from the text you quoted?   You just
  50. have to give it a body.  
  51.  
  52. Giving a body to a pure-virtual method is *very* common and useful.
  53. Making the destructor pure-virtual is a convention many C++
  54. programmers use when they want the base class abstract, but every
  55. base-class method has a meaningful implementation.  In other words,
  56. you don't want every derived class to have to type the same code for
  57. FuncX() so you don't dare make it pure-virtual, well make the dtor
  58. pure virtual.  
  59.  
  60. The following code compiles for me.  If it doesn't for you, I'd
  61. upgrade your compiler.  
  62.  
  63. class A
  64. {
  65. public:
  66.     A();
  67.     virtual ~A() = 0;
  68.  
  69.     virtual void print()
  70.     { cout << "A::print()\n"; }
  71. };
  72.  
  73. class B : public A
  74. {
  75. public:
  76.     B();
  77.     virtual ~B();
  78.  
  79.     virtual void print()
  80.     { cout << "B::print()\n"; }
  81. };
  82.  
  83. A::A()
  84. { NULL; }
  85.  
  86. A::~A()
  87. { cout << "~A\n"; }
  88.  
  89. B::B() : A()
  90. { NULL; }
  91.  
  92. B::~B()
  93. { cout << "~B\n"; }
  94.  
  95.                     --Norm 
  96.  
  97.